package com.springboot.wisdom.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.springboot.wisdom.common.common.bean.DeleteDTO;
import com.springboot.wisdom.common.common.bean.MyResult;
import com.springboot.wisdom.serviceApi.system.model.dto.MenuDTO;
import com.springboot.wisdom.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description
 * @Author LY L
 * @Date 2024-11-03
 */
@Api(tags = "系统管理模块【菜单管理】")
@ApiSupport(author = "LY L")
@RestController
@RequestMapping(value = "/system/menu", produces = {"application/json;charset=UTF-8"})
@Slf4j
//@Validated     // springboot 2.5.* 版本需要在类上标注 @Validated 注解校验注解才会生效。
public class MenuController {

    @Resource
    private MenuService menuService;


    // 动态菜单。
    @ApiOperation(value = "动态菜单", notes = "")
    @GetMapping("/tree")
    public MyResult<List<?>> getMenuTree() {
        return menuService.getTree();
    }

    // 菜单列表分页。
    @ApiOperation(value = "菜单列表", notes = "")
    @GetMapping("/list")
    public MyResult<List<?>> list(MenuDTO menuDTO) {
        return menuService.list(menuDTO);
    }


    // 添加。
    @ApiOperation(value = "添加菜单/按钮", notes = "根据父ID判断子元素添加。")
    @PostMapping("/add")
    public MyResult<String> add(@RequestBody @Valid MenuDTO menuDTO) {
        return menuService.add(menuDTO);
    }


    // 修改。
    @ApiOperation(value = "修改菜单/按钮", notes = "根据父ID判断子元素添加。")
    @PostMapping("/edit")
    public MyResult<String> edit(@RequestBody @Valid MenuDTO menuDTO) {
        return menuService.edit(menuDTO);
    }

    // 删除。
    @ApiOperation(value = "单条删除", notes = "")
    @DeleteMapping("/delete/{id}")
    public MyResult<String> delete(@PathVariable String id) {
        return menuService.delete(id);
    }

    // 删除树及其子元素。
    @ApiOperation(value = "树关联删除", notes = "")
    @DeleteMapping("/delete/tree/{id}")
    public MyResult<String> deleteTree(@PathVariable String id) {
        return menuService.deleteTree(id);
    }

    // 批量删除。
    @ApiOperation(value = "批量删除", notes = "")
    @DeleteMapping("/delete")
    public MyResult<String> deleteList(@RequestBody @Validated DeleteDTO<String> dto) {
        return menuService.deleteList(dto);
    }
}
